package com.gk.util.component
{
	import com.gk.app.SYS;
	
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	/**
	 *文本标签类
	 *  
	 * <br/>只针对单行文本，多行可以用textArea
	 * <br/>label的 textField已经公开
	 * @author Nestor
	 * 
	 */	
	public class Label extends Component
	{
		protected var _autoSize:Boolean = false;
		protected var _text:String = "";
		protected var _tf:TextField;
		
		/**
		 * Constructor
		 * @param parent The parent DisplayObjectContainer on which to add this Label.
		 * @param xpos The x position to place this component.
		 * @param ypos The y position to place this component.
		 * @param text The string to use as the initial text in this component.
		 * @param autosize 自动左排  此时setSize无效
		 */
		public function Label(parent:DisplayObjectContainer = null, xpos:Number = 0, ypos:Number =  0, text:String = "",autoSize:Boolean = true)
		{
			_autoSize = autoSize;
			this.text = text;
			super(parent, xpos, ypos);
		}
		
		/**
		 * Initializes the component.
		 */
		override protected function init():void
		{
			super.init();
			mouseEnabled = false;
			mouseChildren = false;
		}
		
		/**
		 * Creates and adds the child display objects of this component.
		 */
		override protected function addChildren():void
		{
			setSize(50,20);
			_tf = new TextField();
			_tf.defaultTextFormat = new TextFormat(SYS.SYS_FONT,SYS.SYS_NORMAL_SIZE);
			_tf.selectable = false;
			_tf.mouseEnabled = false;
			_tf.text = _text;			
			addChild(_tf);
			draw();
		}
		
		
		
		
		///////////////////////////////////
		// public methods
		///////////////////////////////////
		
		/**
		 * Draws the visual ui of the component.
		 */
		override public function draw():void
		{
			super.draw();
			_tf.text = _text;
			if(_autoSize)
			{
				_tf.autoSize = TextFieldAutoSize.LEFT;
				_width = _tf.width;
				dispatchEvent(new Event(Event.RESIZE));
			}
			else
			{
				_tf.autoSize = TextFieldAutoSize.NONE;
				_tf.width = _width;
			}
			_height = _tf.height = _tf.textHeight +4;
			
			graphics.clear();
			graphics.beginFill(0x000000,0.1);
			graphics.drawRect(0,0,_width,_height);
			graphics.endFill();
		}
		
		override public function setSize(w:Number, h:Number):void
		{
			// TODO Auto Generated method stub
			super.setSize(w, _height);
		}
		
		
		
		
		///////////////////////////////////
		// event handlers
		///////////////////////////////////
		
		///////////////////////////////////
		// getter/setters
		///////////////////////////////////
		
		/**
		 * Gets / sets the text of this Label.
		 */
		public function set text(t:String):void
		{
			_text = t;
			if(_text == null) _text = "";
			if(_tf != null) draw();
		}
		public function get text():String
		{
			return _text;
		}
		
		public function set autoSize(v:Boolean):void
		{
			_autoSize = v;
			invalidate();
		}
		public function get autoSize():Boolean
		{
			return _autoSize;
		}
		
		/**
		 * Gets / sets the internal TextField of the label if you need to do further customization of it.
		 */
		public function get textField():TextField
		{
			return _tf;
		}

		public function set textField(value:TextField):void
		{
			_tf = value;
		}

		
	}
}